<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Models\Product;
use App\Models\User;
use Illuminate\Http\Request;

//购物车

class CartController extends Controller
{
    //购物车列表
    function shopping_cart(Request $request) {
        $request->validate([
           'api_token'  =>  'required|string'
        ]);
        $user_model = new User();
        $user = $user_model->get_login_user($request->get('api_token'));

        $carts= Cart::leftJoin('products','carts.product_id','=','products.id')
                        ->leftJoin('product_skus','carts.product_sku_id','=','product_skus.id')
                        ->where('carts.user_id',$user->id)
                        ->select('products.title as product_title','products.keyword','products.description','product_skus.title as sku_title','products.cover','products.id as product_id','product_skus.id as sku_id','sold_price','stock','carts.buynum','carts.id')
                        ->get()
                        ->toArray();
        return $this->response(0,$carts);
    }


    function cart() {
        return $this->response(0);
    }
    //添加购物车 todo 扣除商品库存
    function add_cart(Request $request) {
        $request->validate([
            'api_token'     =>  'required|string',
            'product_id'    =>  'required|integer|gt:0',
            'sku_id'        =>  'required|integer|gt:0',
            //购买数量
            'buynum'        =>  'required|integer|gt:0',
        ]);

        $api_token = $request->post('api_token');
        $product_id = $request->post('product_id');
        $sku_id = $request->post('sku_id');
        //购买数量
        $buynum = $request->post('buynum');
        $user_model = new User();
        $user = $user_model->get_login_user($api_token);
        //判断商品的规格型号信息
        $product_sku = Product::leftJoin('product_skus','products.id','=','product_skus.product_id')
                                ->where('products.id',$product_id)->where('product_skus.id',$sku_id)
                                ->whereNull('product_skus.deleted_at')
                                ->select('product_skus.stock')->first();
        if (!$product_sku) {
            return $this->response(2,[],'商品信息错误');
        }
        if ($product_sku->stock < $buynum) {
            return $this->response(2,[],'库存不足');
        }

        $where = [
            'user_id'       =>  $user->id,
            'product_id'    =>  $product_id,
            'product_sku_id'        =>  $sku_id
        ];
        $cart = Cart::where($where)->select('id','buynum')->first();

        $insert = null;
        $updated = false;
        if (is_null($cart)) {
            $insert = Cart::create([
                'user_id'       =>  $user->id,
                'product_id'    =>  $product_id,
                'product_sku_id'        =>  $sku_id,
                'buynum'       =>  $buynum
            ]);
        }else {
            $cart->buynum += $buynum;
            $updated = $cart->save();
        }
        if ($insert || $updated) {
            return $this->response(0,[],'添加成功');
        }else {
            return $this->response(2,[],'添加失败');
        }
    }

    //移除购物车
    function delete_cart(Request $request) {
        $request->validate([
            'api_token' =>  'required|string',
            'cart_ids'   =>  'required|array'
        ]);

        $api_token = $request->post('api_token');
        $cart_ids = $request->post('cart_ids');

        $user_model = new User();
        $user = $user_model->get_login_user($api_token);

        Cart::where('user_id', $user->id)->whereIn('id',$cart_ids)->delete();

        return $this->response(0, [], '删除成功');
    }

}
